package com.feihong.txgw.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.LogUtils;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.adapter.Holder.HomeAdViewHolder;
import com.feihong.txgw.adapter.Holder.HomeGridViewHolder;
import com.feihong.txgw.adapter.Holder.HomeMenuHolder;
import com.feihong.txgw.adapter.Holder.HomeTopBannerViewHolder;
import com.feihong.txgw.adapter.Holder.UnnkownHolder;
import com.feihong.txgw.app.App;
import com.feihong.txgw.json.HomeJSON;
import com.feihong.txgw.model.PRODUCT_TYPE;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sabirjan on 2017/6/15.
 * TXGW.
 * 飞宏电子
 */

public class HomeRecyclerAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private static final int TYPE_SLIDER = 100;
    private static final int TYPE_MENU = 101;
    private static final int TYPE_GRID = 102;

    LayoutInflater inflater;
    private Context mContext;
    private HomeJSON.HomeData homeJSON;
    private List<HomeJSON.HomeDataBean> datas = new ArrayList<>();

    public HomeRecyclerAdapter(Context context, HomeJSON.HomeData json) {
        mContext = context;
        this.homeJSON = json;
        if (homeJSON != null) {
            this.datas = this.homeJSON.getData();
        }

        inflater = LayoutInflater.from(context);
    }

    public void setNewDatas(HomeJSON.HomeData json){
        if(json == null){
            throw new NullPointerException("不能为空");
        }
        homeJSON = json;
        datas = homeJSON.getData();
        notifyDataSetChanged();
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_SLIDER;
        } else if (position == 1) {
            return TYPE_MENU;
        } else {
            position = position - 2;
            String type = this.datas.get(position).getList_type();
            if (type == null || type.trim().length() == 0) {
                return PRODUCT_TYPE.TYPE_UNNKOWN;
            }
            if (this.datas.get(position).getItems() == null || this.datas.get(position).getItems().size() == 0) {
                return PRODUCT_TYPE.TYPE_UNNKOWN;
            }
            if (type.toLowerCase().contentEquals(PRODUCT_TYPE.TAG_PHONE)) {
                return PRODUCT_TYPE.TYPE_PHONE;
            } else if (type.toLowerCase().contentEquals(PRODUCT_TYPE.TAG_PHONE_NUM)) {
                return PRODUCT_TYPE.TYPE_PHONE_NUM;
            } else if (type.toLowerCase().contentEquals(PRODUCT_TYPE.TAG_PHONE_PARTS)) {
                return PRODUCT_TYPE.TYPE_PHONE_PARTS;
            } else if (type.toLowerCase().contentEquals(PRODUCT_TYPE.TAG_PHONE_TARIFF)) {
                return PRODUCT_TYPE.TYPE_PHONE_TARIFF;
            } else if (type.toLowerCase().contentEquals(PRODUCT_TYPE.TAG_ADV)) {
                return PRODUCT_TYPE.TYPE_ADV;
            }
        }
        return PRODUCT_TYPE.TYPE_UNNKOWN;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_SLIDER) {
            View view = inflater.inflate(R.layout.topbanner, parent, false);
            HomeTopBannerViewHolder holder = new HomeTopBannerViewHolder(mContext, view);
            return holder;
        } else if (viewType == TYPE_MENU) {
            View view = inflater.inflate(R.layout.holder_home_menu, parent, false);
            HomeMenuHolder holder = new HomeMenuHolder(mContext, view);
            return holder;
        } else if (viewType == PRODUCT_TYPE.TYPE_UNNKOWN) {
            View view = inflater.inflate(R.layout.unnkownlayout, parent, false);
            UnnkownHolder unnkownHolder = new UnnkownHolder(view);
            return unnkownHolder;
        } else if (viewType == PRODUCT_TYPE.TYPE_ADV) {
            View view = inflater.inflate(R.layout.home_holder_adv, parent, false);
            HomeAdViewHolder adViewHolder = new HomeAdViewHolder(view);
            return adViewHolder;
        }else if(viewType == PRODUCT_TYPE.TYPE_PHONE){
            return new HomePhoneViewHolder(mContext,LayoutInflater.from(mContext).inflate(R.layout.home_phone_viewholder_layout,parent,false));
        } else {
            View view = inflater.inflate(R.layout.holder_home_gridview, parent, false);
            HomeGridViewHolder holder = new HomeGridViewHolder(mContext, viewType, view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (holder instanceof HomeTopBannerViewHolder) {
            ((HomeTopBannerViewHolder) holder).bindData();
        } else if (holder instanceof HomeGridViewHolder) {
            HomeGridViewHolder homeGridViewHolder = (HomeGridViewHolder) holder;
            homeGridViewHolder.bindData(this.datas.get(position - 2));//去掉上面的两层
        } else if (holder instanceof HomeAdViewHolder) {

        } else if (holder instanceof HomeMenuHolder) {
            HomeMenuHolder homeMenuHolder = (HomeMenuHolder) holder;
            homeMenuHolder.bindData(homeJSON);
        }else if(holder instanceof HomePhoneViewHolder){
            HomePhoneViewHolder mHolder = (HomePhoneViewHolder) holder;
            mHolder.initData(this.datas.get(position - 2));
        }
    }

    @Override
    public int getItemCount() {
        if (homeJSON == null) {
            return 2;
        }
        //获取的数据列表上加2的意思是首页上面的图片区域和栏目区域
        return datas.size() + 2;

    }
}
